// Copyright (c) 2025 Huawei Technologies Co., Ltd.
// openUBMC is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
//         http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
// EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
// MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
//
// Description: 实现kvm客户端认证提供创建会话的能力

#include "boost/utility/string_view_fwd.hpp"
#include "Utils.hpp"
#include "ClientAuthentication.hpp"
static boost::string_view g_createSessionUri = "/UI/REST/KvmHandler";

ClientAuthentication::ClientAuthentication() : ClientBaseHttp(g_createSessionUri),
    succRes_({"",nullptr,nullptr,"",0, {}}), lastSuccToken_(nullptr), lastSuccSessionId_(nullptr)
{
}

ClientAuthentication::~ClientAuthentication()
{
    ResetSuccRes();
    DeleteCharArray(lastSuccToken_);
    DeleteCharArray(lastSuccSessionId_);
}

void ClientAuthentication::GetReqBody(boost::string_view userName, const char* passWord, boost::string_view kvmMode)
{
    try {
        boost::json::object obj_body;
        obj_body["check_pwd"] = passWord;
        obj_body["logtype"] = "1";
        obj_body["user_name"] = userName;
        obj_body["func"] = "DirectKVM";
        obj_body["IsKvmApp"] = "1";
        obj_body["KvmMode"] = kvmMode;
        boost::json::value json_body(obj_body);
        SetBody(boost::json::serialize(json_body).c_str());
    } catch (...) {
        throw;
    }
}

bool ClientAuthentication::CreateSessionFromWeb(boost::string_view host, boost::string_view port,
    boost::string_view userName, const char* passWord)
{
    try {
        return ClientBaseHttp::Call(host, port, userName, passWord, boost::beast::http::verb::post);
    } catch (...) {
        throw;
    }
}

void ClientAuthentication::ParseSuccInfo()
{
    try {
        if (resObj_ == NULL) {
            return;
        }
        if (resObj_.contains("Token")) {
            succRes_.token = DeepCopyCstr(resObj_["Token"].as_string().c_str());
            SafeStringUpdate(lastSuccToken_, resObj_["Token"].as_string().c_str());
        }

        if (resObj_.contains("KvmPort")) {
            succRes_.kvmPort = resObj_["KvmPort"].as_int64();
        }

        if (resObj_.contains("Title")) {
            succRes_.title = resObj_["Title"].as_string().c_str();
        }

        if (resObj_.contains("SessionId")) {
            succRes_.sessionId = DeepCopyCstr(resObj_["SessionId"].as_string().c_str());
            SafeStringUpdate(lastSuccSessionId_, resObj_["SessionId"].as_string().c_str());
        }

        if (resObj_.contains("KvmIP")) {
            succRes_.kvmIp = resObj_["KvmIP"].as_string().c_str();
        }

        auto privilege = resObj_["Privilege"];
        if (privilege.is_array()) {
            auto privilegeArray = privilege.as_array();
            for (auto it = privilegeArray.begin(); it != privilegeArray.end(); ++it) {
                succRes_.privilege.push_back(it->as_string().c_str());
            }
        }
    } catch (...) {
        throw;
    }
}

void ClientAuthentication::ResetSuccRes()
{
    try {
        succRes_.title = "";
        DeleteCharArray(succRes_.sessionId);
        succRes_.kvmIp = "";
        DeleteCharArray(succRes_.token);
        succRes_.kvmPort = 0;
        succRes_.privilege.clear();
    } catch (...) {
        throw;
    }
}

HttpSuccInfo ClientAuthentication::GetSuccInfo()
{
    return succRes_;
}

boost::string_view ClientAuthentication::GetToken()
{
    return lastSuccToken_;
}

boost::string_view ClientAuthentication::GetSessionId()
{
    return lastSuccSessionId_;
}